package com.liu.ssm.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.liu.ssm.controller.service.GradeService;
import com.liu.ssm.entity.Grade;
import com.liu.ssm.page.Page;
import com.liu.ssm.util.StringUtil;

/**
 * 年级信息管理
 * 
 * @author SHIYI
 *
 */
@RequestMapping("/grade")
@Controller
public class GradeController {

	@Autowired
	private GradeService gradeservice;

	/**
	 * 年级管理列表页面
	 * 
	 * @param modviw
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView list(ModelAndView modviw) {
		modviw.setViewName("grade/grade_list");
		return modviw;
	}

	/**
	 * 获取年级列表
	 * 
	 * @param name
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/get_list", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getlist(@RequestParam(value = "name", defaultValue = "", required = false) String name,
			Page page) {
		Map<String, Object> ret = new HashMap<String, Object>();
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("name", "%" + name + "%");
		queryMap.put("offset", page.getOffset());
		queryMap.put("pageSize", page.getRows());
		ret.put("rows", gradeservice.findList(queryMap));
		ret.put("total", gradeservice.getTotal(queryMap));
		return ret;
	}

	/**
	 * 添加年级信息
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> add(Grade grade) {
		Map<String, String> ret = new HashMap<String, String>();
		if (StringUtils.isEmpty(grade.getName())) {
			ret.put("type", "error");
			ret.put("msg", "年级名称不能为空！");
			return ret;
		}
		if (gradeservice.add(grade) <= 0) {
			ret.put("type", "error");
			ret.put("msg", "年级添加失败！");
			return ret;
		}
		ret.put("type", "success");
		ret.put("msg", "添加成功！");
		return ret;
	}

	/**
	 * 修改年级信息
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> edit(Grade grade) {
		Map<String, String> ret = new HashMap<String, String>();
		if (StringUtils.isEmpty(grade.getName())) {
			ret.put("type", "error");
			ret.put("msg", "年级名称不能为空！");
			return ret;
		}
		if (gradeservice.edit(grade) <= 0) {
			ret.put("type", "error");
			ret.put("msg", "年级修改失败！");
			return ret;
		}
		ret.put("type", "success");
		ret.put("msg", "年级修改成功！");
		return ret;
	}

	/**
	 * 删除年级信息
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> delete(@RequestParam(value = "ids[]", required = true) Long[] ids) {
		Map<String, String> ret = new HashMap<String, String>();
		if (ids == null || ids.length == 0) {
			ret.put("type", "error");
			ret.put("msg", "请选择要删除的数据！");
			return ret;
		}
		try {
			if (gradeservice.delete(StringUtil.joinString(Arrays.asList(ids), ",")) <= 0) {
				ret.put("type", "error");
				ret.put("msg", "删除失败！");
				return ret;
			}
		} catch (Exception e) {
			// TODO: handle exception
			ret.put("type", "error");
			ret.put("msg", "该年级下存在班级信息，请勿冲动！");
			return ret;
		}
		ret.put("type", "success");
		ret.put("msg", "年级删除成功！");
		return ret;
	}

}
